package com.future.mayi.utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import java.util.HashMap;
import java.util.Map;


public class MyWebViewClient extends WebViewClient {

    ProgressBar mProgressBar;
    Context context;
    private Map<String, String> headers;


    public MyWebViewClient(ProgressBar mProgressBar, Context context) {
        super();
        this.mProgressBar = mProgressBar;
        this.context = context;

        headers = new HashMap<>();
    }


    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {


        return super.shouldInterceptRequest(view, request);
    }


    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        return super.shouldOverrideUrlLoading(view, request);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("tel:")) {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            context.startActivity(intent);
        } else {
            view.loadUrl(url, headers);
        }
        return false;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        if (mProgressBar != null)
            mProgressBar.setVisibility(View.GONE);
        super.onPageFinished(view, url);
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        if (mProgressBar != null)
            mProgressBar.setVisibility(View.VISIBLE);
        super.onPageStarted(view, url, favicon);
    }






}
